package dynamic_hybrid;


public class RuleMatrix extends Matrix
{
	private int RULENUMBER,ROW,COLUMN,BLOCKROW,BLOCKCOLUMN ;
	private int[] BLOCKVALUES = new int[9] ;
	
	private int[][] RULEMATRIX ;
	
	private String _binaryvalue ; 
	
	public RuleMatrix(int rulenumber,int row,int column,int blockrow,int blockcolumn)
	{
		this.RULENUMBER   = rulenumber  ;
		this.ROW          = row         ;
		this.COLUMN       = column      ;
		this.BLOCKROW     = blockrow    ;
		this.BLOCKCOLUMN  = blockcolumn ;
		
		blockValues(RULENUMBER) ;
		
		RULEMATRIX = defineTransitionMatrix() ;
	}
	
	public int[][] getRuleMatrix()
	{
		return this.RULEMATRIX ;
	}
	
	
		
	private void blockValues(int number)
	{
	      String binary     = Integer.toBinaryString(number) ;
	      int binaryLength  = binary.length() ;
          this._binaryvalue = binary ;  
	      
	      for(int i = 0; i< binaryLength;i++)
	          BLOCKVALUES[i] = Integer.parseInt(Character.toString(binary.charAt(binaryLength-(i+1)))) ;
    
	}
	
	
	
	
	
	private  int[][] blockArray(int alfa ,int beta,int teta)
	  {	      
	      	
	      int[][] result = new int[BLOCKROW][BLOCKCOLUMN];
	
	      for (int i =0 ; i< BLOCKROW;i++ )
	          for(int j = 0 ; j < BLOCKCOLUMN;j++)
	          {
	              result[i][i] = alfa ;
	              result[i][(i+1)%BLOCKCOLUMN] = beta;
	              result[i][(i+(BLOCKCOLUMN-1))%BLOCKCOLUMN] = teta;
	          }	      
	      return result;
	  }
	
	private  int[][] defineTransitionMatrix()
	  {
	      	
	      int alpha,beta,phei,gamma,delta,psei,theta,omega,mu ;
	      int[][] result       = new int[ROW][COLUMN] ;
	      
	     	      
	      alpha = BLOCKVALUES[0] ;
	      beta  = BLOCKVALUES[1] ;
	      delta = BLOCKVALUES[2] ;
	      gamma = BLOCKVALUES[3] ;
	      psei  = BLOCKVALUES[4] ;
	      phei  = BLOCKVALUES[5] ;
	      mu    = BLOCKVALUES[6] ;
	      theta = BLOCKVALUES[7] ;
	      omega = BLOCKVALUES[8] ;
	      
	      	      
	      int[][] alphaMatrix  = this.blockArray(alpha, beta, phei) ;
	      int[][] gammaMatrix  = this.blockArray(gamma,delta,psei)  ;
	      int[][] thetaMatrix  = this.blockArray(theta,omega,mu)    ;
	      
	     /* printMatrix(alphaMatrix, " ALPHA Beta phei MATRIX FOR :" +this.RULENUMBER) ;
	      printMatrix(gammaMatrix, " gamma delta psei MATRIX FOR :") ;
	      printMatrix(thetaMatrix, " theta mega  mu MATRIX FOR :") ;*/
	
	      for(int i = 0,j=0 ; i< ROW; i += BLOCKROW , j += BLOCKCOLUMN )
	      {	                  
	          for(int k = 0 ; k <  BLOCKROW ; k++)
	          {
	                
		          System.arraycopy(alphaMatrix[k], 0,result[i+k], (j%COLUMN) ,  BLOCKCOLUMN) ;
		          System.arraycopy(gammaMatrix[k], 0,result[i+k], ((j+BLOCKCOLUMN)%COLUMN) ,BLOCKCOLUMN) ;
		          System.arraycopy(thetaMatrix[k], 0,result[i+k], ((j+(COLUMN - BLOCKCOLUMN))%COLUMN) ,BLOCKCOLUMN) ;
			         
	          }             
	       }
	      
	      return result ;
	
	  }
	
	public String toString()
	{
		int alpha,beta,phei,gamma,delta,psei,theta,omega,mu ;  
		
		alpha = BLOCKVALUES[0] ;
	    beta  = BLOCKVALUES[1] ;
	    delta = BLOCKVALUES[2] ;
	    gamma = BLOCKVALUES[3] ;
	    psei  = BLOCKVALUES[4] ;
	    phei  = BLOCKVALUES[5] ;
	    mu    = BLOCKVALUES[6] ;
	    theta = BLOCKVALUES[7] ;
	    omega = BLOCKVALUES[8] ;
	    
	    System.out.println("Binary value for rulenumber " + this.RULENUMBER + " is :"+this._binaryvalue);
	    System.out.println("Block Values for "+this.RULENUMBER +"; alpha:"+alpha+" ,beta :"+beta+" ,phei:"+phei+" ,gamma:"+gamma+" ,delta:"+delta+" ,psei:"+psei+" ,theta:"+theta+" ,omega:"+omega+" ,mu:"+mu);
	    		
		printMatrix(RULEMATRIX, "RESULT MATRIX FOR RULE "+ this.RULENUMBER) ;
		
		return " " ;
	}
	
}
